package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.shopping.CartListForm;
import com.cskaoyan.gateway.form.shopping.UpdateCartListForm;
import com.cskaoyan.gateway.utils.GetJwtUtils;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartListService;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.IMemberService;
import com.mall.user.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "CartListController", description = "购物车")
public class CartListController {

    @Reference
    ICartListService iCartListService;

    @Reference(check = false)
    IMemberService iMemberService;

    @Reference
    ICartService iCartService;

    @ApiOperation("显示购物车")
    @GetMapping("/carts")
    public ResponseData cartsList(HttpServletRequest servletRequest){
       // AddAddressRequest request = new AddAddressRequest();

        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
        //从httprequset里拿cookie -->access_token
        String jwt = new GetJwtUtils().getJwt(servletRequest);
        if (!jwt.equals(null)){
            checkAuthRequest.setToken(jwt);
        }
        //进行处理然后拿到userinfo
        CheckAuthResponse checkAuthResponse = iMemberService.validToken(checkAuthRequest);
        String userinfo = checkAuthResponse.getUserinfo();
        CartListByIdRequest cartListShowRequest = new CartListByIdRequest();
        if (userinfo!=null){
        Integer id = iMemberService.cartSelectById(userinfo);
        Long realId = Long.parseLong(id.toString());
        cartListShowRequest.setUserId(realId);
        //cartListShowRequest.setUserInfo(userInfo);
        }

        List<CartListShowTwoDTO> cartListShow = iCartListService.getCartListShow(cartListShowRequest);

        //CartListByIdResponse cartListByIdResponse1 = iCartService.getCartListById(cartListShowRequest);
        ResponseData responseData = new ResponseData();

        responseData.setCode(200);
        responseData.setMessage("success");
        responseData.setResult(cartListShow);
        responseData.setSuccess(true);
        responseData.setTimestamp(System.currentTimeMillis());

        return responseData;
    }

    @ApiOperation("增加购物车")
    @PostMapping("/carts")
    public ResponseData addCartsList(@RequestBody CartListForm cartListForm){

        AddCartRequest request = new AddCartRequest();
        request.setItemId(cartListForm.getProductId());
        request.setNum(cartListForm.getProductNum());
        request.setUserId(cartListForm.getUserId());

        AddCartResponse response = iCartService.addToCart(request);

        ResponseData responseData = new ResponseData();
        responseData.setTimestamp(System.currentTimeMillis());
        responseData.setSuccess(true);
        responseData.setResult("成功");
        responseData.setMessage("success");
        responseData.setCode(200);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @ApiOperation("删除购物车")
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData deleteCartsList(@PathVariable("uid")Long uid,@PathVariable("pid")Long pid){

        DeleteCartItemRequest request = new DeleteCartItemRequest(uid,pid);
        request.setItemId(pid);
        request.setUserId(uid);

        DeleteCartItemResponse response = iCartService.deleteCartItem(request);

        ResponseData responseData = new ResponseData();
        responseData.setTimestamp(System.currentTimeMillis());
        responseData.setSuccess(true);
        responseData.setResult("成功");
        responseData.setMessage("success");
        responseData.setCode(200);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


    @ApiOperation("删除购物⻋中选中的商品")
    @DeleteMapping ("/items/{id}")
    public ResponseData updateCartsList(@PathVariable("id")Long id){

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        //这个商品ID要从别的地方去取
        request.setUserId(id);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @ApiOperation("全选")
    @PutMapping ("/items")
    public ResponseData itemsCartsList(@RequestBody CheckAllItemRequest checkAllItemRequest){

        CheckAllItemResponse response = iCartService.checkAllCartItem(checkAllItemRequest);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @ApiOperation("更新购物车")
    @PutMapping ("/carts")
    public ResponseData updateCartsList(@RequestBody UpdateCartListForm updateCartListForm){
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setChecked(updateCartListForm.getChecked());
        request.setItemId(updateCartListForm.getProductId());
        request.setNum(updateCartListForm.getProductNum());
        request.setUserId(updateCartListForm.getUserId());

        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
